package com.njtoyo.taxi.admin.logic.data;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njtoyo.taxi.admin.handler.FilterHandler;
import com.njtoyo.taxi.admin.mapper.secondary.entity.data.DashboardCallDaily;
import com.njtoyo.taxi.admin.mapper.secondary.entity.data.DashboardCallMinute;
import com.njtoyo.taxi.admin.mapper.secondary.mapper.data.DashboardCallDailyMapper;
import com.njtoyo.taxi.admin.mapper.secondary.mapper.data.DashboardCallMinuteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class DashboardCallLogic {

    @Autowired
    private DashboardCallMinuteMapper dashboardCallMinuteMapper;

    @Autowired
    private DashboardCallDailyMapper dashboardCallDailyMapper;

    public List<DashboardCallMinute> getCallCountFromMinuteGroupByCanton(Date startTime, Date endTime) {
        QueryWrapper<DashboardCallMinute> queryWrapper = new QueryWrapper<>();

        queryWrapper.select("IFNULL(SUM(num), 0) as total, canton_id");

        String start = DateUtil.formatDateTime(startTime);
        String end = DateUtil.formatDateTime(endTime);

        queryWrapper.between("stats_date", start, end);
        queryWrapper.isNotNull("canton_id");
        queryWrapper.groupBy("canton_id");

        return dashboardCallMinuteMapper.selectList(queryWrapper);
    }

    public List<DashboardCallDaily> getCallCountFromDailyGroupByCanton(Date startTime, Date endTime) {
        QueryWrapper<DashboardCallDaily> queryWrapper = new QueryWrapper<>();

        queryWrapper.select("IFNULL(SUM(num), 0) as total, canton_id");

        String start = DateUtil.formatDate(startTime);
        String end = DateUtil.formatDate(endTime);

        queryWrapper.between("stats_date", start, end);
        queryWrapper.isNotNull("canton_id");
        queryWrapper.groupBy("canton_id");

        return dashboardCallDailyMapper.selectList(queryWrapper);
    }

    public List<Map<String, Object>> getCallCountFromDailyGroupByDate(FilterHandler filterHandler, Date startTime, Date endTime) {
        QueryWrapper<DashboardCallDaily> callWrapper = new QueryWrapper<>();
        switch (filterHandler.getFilterType()) {
            case canton:
            case company:
                callWrapper.in("canton_id", filterHandler.getCantonIds());
        }
        callWrapper.between("stats_date", startTime, endTime)
                .select("stats_date, SUM(num) as total")
                .groupBy("stats_date");

        List<Map<String, Object>> result = dashboardCallDailyMapper.selectMaps(callWrapper);
        // 翻倍率
        Double ride = filterHandler.getFakeMultiple().getCall();
        if (Objects.nonNull(ride)) {
            for (int i = 0; i < result.size(); i++) {
                Map<String, Object> item = result.get(i);
                item.put("total", Math.round(Long.parseLong(item.get("total").toString()) * ride));
                result.set(i, item);
            }
        }

        return result;
    }
}
